package com.example.springblog.config;

import com.example.springblog.constants.Constant;
import com.example.springblog.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@Configuration
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader(Constant.USER_TOKEN_HEADER);
        log.info("获取到token" + token);
        boolean result = JwtUtils.checkToken(token);
        if (result) return true;
        response.setStatus(401);
        return false;
    }
}
